/**
 * 
 */
package net.ddns.heroli.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.gzheroli.bfw.pojo.base.TSUser;
import cn.com.gzheroli.cache.SpringRedisCache;
import cn.com.gzheroli.core.common.model.json.AjaxJson;
import cn.com.gzheroli.core.common.model.json.TreeGrid;
import cn.com.gzheroli.core.common.service.CommonService;
import cn.com.gzheroli.core.common.service.HeroService;
import cn.com.gzheroli.core.common.service.SystemService;
import cn.com.gzheroli.core.manager.Client;
import cn.com.gzheroli.core.util.IdWorker;
import cn.com.gzheroli.core.util.NameHandler;
import cn.com.gzheroli.core.util.RequestUtil;
import cn.com.gzheroli.util.GenSqlUtil;
import net.ddns.heroli.model.OrderModel;
import net.ddns.heroli.pojo.TBGoods;
import net.ddns.heroli.pojo.TBGoodsAttr;
import net.ddns.heroli.pojo.TBGoodsType;
import net.ddns.heroli.pojo.TBOrder;
import net.ddns.heroli.pojo.TBOrderGoods;

/**  
* 类名: GoodsController  
* 描述: TODO(这里用一句话描述这个类的作用)  
* 作者: 李长荣 lichang_20011@163.com  
* 日期: 2017年12月18日  下午12:47:09
*@version V1.0  
*    
*/
@Controller
@RequestMapping("/api/orderController")
public class OrderController {
	private static final Logger logger = LoggerFactory.getLogger(OrderController.class);
	@Autowired
	private CommonService commonService;
	@Autowired
	private HeroService heroService;
	@Autowired
    private SpringRedisCache cache;
	@Autowired
	private SystemService systemService;
	@RequestMapping(value = "getOrder")
	@ResponseBody
	public AjaxJson getOrder(String id, HttpServletRequest request) {
		
		AjaxJson j = new AjaxJson();
		if(id==null||id.trim().equals("")){
			j.setSuccess(false);
			j.setMsg("id不存在!");
			return j;
		}
		Map paras=new HashMap();
		paras.put("id", id);
		Map order=heroService.findForSqlIdSingle("order.getOrder", paras, Map.class);
		List goodsOrderList=heroService.findForSqlId("order.getOrderGoodsList",paras,Map.class);
		if(order!=null&&!order.isEmpty()) {
			order.put("goodsOrderList", goodsOrderList);
		}
		j.setObj(order);
		j.setSuccess(true);
		j.setMsg("删除成功!");
		return j;
	}
	@RequestMapping(value = "delOrder")
	@ResponseBody
	public AjaxJson delOrder(String id, HttpServletRequest request) {
		
		AjaxJson j = new AjaxJson();
		if(id==null||id.trim().equals("")){
			j.setSuccess(false);
			j.setMsg("id不存在!");
			return j;
		}
		String token = request.getHeader("token")==null?request.getParameter("token"):request.getHeader("token");
		Client client=cache.getCache(token, Client.class);
		if(client==null||client.getUser()==null){
			j.setSuccess(false);
			j.setMsg("尚未登录,请登录!");
			return j;
		}
		TSUser user=client.getUser();
		TBOrder order=commonService.get(TBOrder.class, id);
		order.setUpdateBy(user.getId());
		order.setUpdateTime(new Date());
		order.setUpdateByName(user.getRealName());
		order.setStatus("0");
		commonService.update(order);
		j.setSuccess(true);
		j.setMsg("删除成功!");
		return j;
	}
	@RequestMapping(value = "saveOrder")
	@ResponseBody
	public  AjaxJson saveOrder(TBOrder order,OrderModel orderModel, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		String token = request.getHeader("token")==null?request.getParameter("token"):request.getHeader("token");
		Client client=cache.getCache(token, Client.class);
		if(client==null||client.getUser()==null){
			j.setSuccess(false);
			j.setMsg("尚未登录,请登录!");
			return j;
		}
		TSUser user=client.getUser();
		List<TBOrderGoods> orderGoods=orderModel.getOrderGoods();
		if(order.getId()==null){
			String id=IdWorker.getFlowIdWorkerInstance().nextIdStr();
			order.setUpdateBy(user.getId());
			order.setUpdateTime(new Date());
			order.setUpdateByName(user.getRealName());
			order.setId(id);
			order.setStatus("1");
			commonService.save(order);
			if(orderGoods!=null&&!orderGoods.isEmpty()) {
				for(TBOrderGoods orderGood:orderGoods) {
					orderGood.setOrderId(id);
					orderGood.setUpdateBy(user.getId());
					orderGood.setUpdateTime(new Date());
					orderGood.setUpdateByName(user.getRealName());
					orderGood.setStatus("1");
					commonService.save(orderGood);
				}
			}
		}else{
			order.setUpdateBy(user.getId());
			order.setUpdateTime(new Date());
			order.setUpdateByName(user.getRealName());
			commonService.update(order);
			if(orderGoods!=null&&!orderGoods.isEmpty()) {
				for(TBOrderGoods orderGood:orderGoods) {
					orderGood.setOrderId(order.getId());
					orderGood.setUpdateBy(user.getId());
					orderGood.setUpdateTime(new Date());
					orderGood.setUpdateByName(user.getRealName());
					//orderGood.setStatus("1");
					commonService.save(orderGood);
				}
			}
		}
		j.setSuccess(true);
		j.setMsg("保存成功!");
		return j;
	}
	@RequestMapping(value = "updateOrder")
	@ResponseBody
	public  AjaxJson update( HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		String token = request.getHeader("token")==null?request.getParameter("token"):request.getHeader("token");
		Client client=cache.getCache(token, Client.class);
		if(client==null||client.getUser()==null){
			j.setSuccess(false);
			j.setMsg("尚未登录,请登录!");
			return j;
		}
		String id=request.getParameter("id")==null?"":request.getParameter("id");
		TSUser user=client.getUser();
		if(id==null||id.trim().equals("")){
			j.setSuccess(false);
			j.setMsg("保存失败!,编号不能为空");
			return j;
		}else{
			Map reqMap=RequestUtil.convertParameterToMap(request);
			reqMap.put("updateBy", user.getId());
			reqMap.put("updateTime", new Date());
			reqMap.put("updateByName", user.getRealName());
			heroService.updateMap("T_B_ORDER", reqMap, null, TBOrder.class);
		}
		j.setSuccess(true);
		j.setMsg("保存成功!");
		return j;
	}
	@RequestMapping(value = "listOrder")
	@ResponseBody
	public  AjaxJson  listOrder(HttpServletRequest request, HttpServletResponse response, TreeGrid treegrid) {
		AjaxJson j = new AjaxJson();
		String page=request.getParameter("page")==null?"1":request.getParameter("page");
		String rows=request.getParameter("rows")==null?"1000":request.getParameter("rows");
		//Client client=cache.getCache(request.getSession().getId(), Client.class);
		String token = request.getHeader("token")==null?request.getParameter("token"):request.getHeader("token");
		Client client=cache.getCache(token, Client.class);
		if(client==null||client.getUser()==null){
			//return null;
		}else{
			TSUser user=client.getUser();
		}
		Map reqMap=RequestUtil.convertParameterToMap(request);
		List<Map<String,Object>> treeGrids = new ArrayList<Map<String,Object>>();
		Map conMap=new HashMap();
		conMap.put("status", "=");
		if(!reqMap.containsKey("status"))
			reqMap.put("status", "1");
		//if(reqMap.containsKey("projectType"))
		//conMap.put("projectType", "=");
		if(reqMap.containsKey("name"))
		conMap.put("name", "like");
		if(reqMap.containsKey("code"))
			conMap.put("code", "like");
		Class clazz=TBOrder.class;
		String tableName = new NameHandler().getTableName(clazz.getSimpleName());
		Map<String,Object> tableColumns=systemService.getHeroTableColumns(tableName,clazz);
		String whereSql="";
		whereSql=GenSqlUtil.genWhereSql(tableName, conMap, tableColumns, "o", reqMap);
		//Map<String,Object> userColumns=systemService.getHeroTableColumns("T_S_USER",TSUser.class);
		//whereSql=whereSql+GenSqlUtil.genWhereSql("T_S_USER", conMap, userColumns, "u", reqMap);
		List whereSqlList=new ArrayList();
		whereSqlList.add(" 1=1 "+whereSql);
		reqMap.put("whereSql", " 1=1 "+whereSql);
		PageQuery pg=heroService.findForSqlIdPage("order.getOrderList", reqMap,Long.parseLong(page),Long.parseLong(rows));
		j.setObj(pg.getList());
		j.setTotal(pg.getTotalRow());
		return j;
	}
	
	@RequestMapping(value = "delOrderGoods")
	@ResponseBody
	public AjaxJson delOrderGoods(String id, HttpServletRequest request) {
		
		AjaxJson j = new AjaxJson();
		if(id==null||id.trim().equals("")){
			j.setSuccess(false);
			j.setMsg("id不存在!");
			return j;
		}
		String token = request.getHeader("token")==null?request.getParameter("token"):request.getHeader("token");
		Client client=cache.getCache(token, Client.class);
		if(client==null||client.getUser()==null){
			j.setSuccess(false);
			j.setMsg("尚未登录,请登录!");
			return j;
		}
		TSUser user=client.getUser();
		TBOrderGoods orderGoods=commonService.get(TBOrderGoods.class, id);
		orderGoods.setUpdateBy(user.getId());
		orderGoods.setUpdateTime(new Date());
		orderGoods.setUpdateByName(user.getRealName());
		orderGoods.setStatus("0");
		commonService.update(orderGoods);
		j.setSuccess(true);
		j.setMsg("删除成功!");
		return j;
	}
	@RequestMapping(value = "saveOrderGoods")
	@ResponseBody
	public  AjaxJson saveOrderGoods(TBOrderGoods orderGoods, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		String token = request.getHeader("token")==null?request.getParameter("token"):request.getHeader("token");
		Client client=cache.getCache(token, Client.class);
		if(client==null||client.getUser()==null){
			j.setSuccess(false);
			j.setMsg("尚未登录,请登录!");
			return j;
		}
		TSUser user=client.getUser();
		if(orderGoods.getId()==null){
			orderGoods.setUpdateBy(user.getId());
			orderGoods.setUpdateTime(new Date());
			orderGoods.setUpdateByName(user.getRealName());
			orderGoods.setStatus("1");
			commonService.save(orderGoods);
		}else{
			orderGoods.setUpdateBy(user.getId());
			orderGoods.setUpdateTime(new Date());
			orderGoods.setUpdateByName(user.getRealName());
			commonService.update(orderGoods);
		}
		j.setSuccess(true);
		j.setMsg("保存成功!");
		return j;
	}
	@RequestMapping(value = "updateOrderGoods")
	@ResponseBody
	public  AjaxJson updateOrderGoods( HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		String token = request.getHeader("token")==null?request.getParameter("token"):request.getHeader("token");
		Client client=cache.getCache(token, Client.class);
		if(client==null||client.getUser()==null){
			j.setSuccess(false);
			j.setMsg("尚未登录,请登录!");
			return j;
		}
		String id=request.getParameter("id")==null?"":request.getParameter("id");
		TSUser user=client.getUser();
		if(id==null||id.trim().equals("")){
			j.setSuccess(false);
			j.setMsg("保存失败!,编号不能为空");
			return j;
		}else{
			Map reqMap=RequestUtil.convertParameterToMap(request);
			reqMap.put("updateBy", user.getId());
			reqMap.put("updateTime", new Date());
			reqMap.put("updateByName", user.getRealName());
			heroService.updateMap("T_B_ORDER_GOODS", reqMap, null, TBOrderGoods.class);
		}
		j.setSuccess(true);
		j.setMsg("保存成功!");
		return j;
	}
	@RequestMapping(value = "listOrderGoods")
	@ResponseBody
	public  AjaxJson  listOrderGoods(HttpServletRequest request, HttpServletResponse response, TreeGrid treegrid) {
		AjaxJson j = new AjaxJson();
		String page=request.getParameter("page")==null?"1":request.getParameter("page");
		String rows=request.getParameter("rows")==null?"1000":request.getParameter("rows");
		Map reqMap=RequestUtil.convertParameterToMap(request);
		List<Map<String,Object>> treeGrids = new ArrayList<Map<String,Object>>();
		Map conMap=new HashMap();
		conMap.put("status", "=");
		if(!reqMap.containsKey("status"))
			reqMap.put("status", "1");
		//if(reqMap.containsKey("projectType"))
		//conMap.put("projectType", "=");
		if(reqMap.containsKey("name"))
		conMap.put("name", "like");
		if(reqMap.containsKey("code"))
			conMap.put("code", "like");
		Class clazz=TBOrderGoods.class;
		String tableName = new NameHandler().getTableName(clazz.getSimpleName());
		Map<String,Object> tableColumns=systemService.getHeroTableColumns(tableName,clazz);
		String whereSql="";
		whereSql=GenSqlUtil.genWhereSql(tableName, conMap, tableColumns, "og", reqMap);
		//Map<String,Object> userColumns=systemService.getHeroTableColumns("T_S_USER",TSUser.class);
		//whereSql=whereSql+GenSqlUtil.genWhereSql("T_S_USER", conMap, userColumns, "u", reqMap);
		List whereSqlList=new ArrayList();
		whereSqlList.add(" 1=1 "+whereSql);
		reqMap.put("whereSql", " 1=1 "+whereSql);
		PageQuery pg=heroService.findForSqlIdPage("order.getOrderGoodsList", reqMap,Long.parseLong(page),Long.parseLong(rows));
		j.setObj(pg.getList());
		j.setTotal(pg.getTotalRow());
		return j;
	}
}
